﻿namespace _1.简单工厂模式;

public class OperationFactory
{
    /// <summary>
    /// 根据传入的运算符，创建对应的运算方法对象，返回给调用者
    /// </summary>
    /// <param name="operate">运算符</param>
    /// <returns>运算方法对象</returns>
    /// <exception cref="运算符不合法"></exception>
    public static Operation CreateOperate(string operate)
    {
        Operation? oper = null;
        switch (operate)
        {
            case "+":
                oper = new OperationAdd();
                break;
            case "-":
                oper = new OperationSub();
                break;
            case "*":
                oper = new OperationMul();
                break;
            case "/":
                oper = new OperationDiv();
                break;
        }
        if (oper == null)
            throw new Exception("运算符不合法");
        return oper;
    }
}
